﻿import mx.controls.Loader;
import mx.containers.ScrollPane;
import mx.utils.Delegate;

import idv.ben.picasawebservice4flash.data.*;
import idv.ben.picasawebservice4flash.sample.*;
import idv.ben.picasawebservice4flash.service.*;

class idv.ben.picasawebservice4flash.sample.SampleAlbum extends MovieClip{
	
	var albums_sp:ScrollPane;
	var photos_sp:ScrollPane;
	
	private var __service:PicasawebService;
	
	function SampleAlbum(){
		__service = new PicasawebService();
		
		__service.addEventListener(PicasawebService.EVENT_FEED_ONLOAD, Delegate.create(this, handlerFeedOnLoad));
		__service.addEventListener(PicasawebService.EVENT_FEED_ONERROR, Delegate.create(this, handlerFeedOnError));

		__service.addEventListener(PicasawebService.EVENT_ENTRY_ONLOAD, Delegate.create(this, handlerEntryOnLoad));
		__service.addEventListener(PicasawebService.EVENT_ENTRY_ONERROR, Delegate.create(this, handlerEntryOnError));
		
		__service.loadFeed("Ben.RB.Chang");
	}
	
	private function handlerFeedOnLoad(evtObj:Object):Void{
		for(var i:String in evtObj){
			trace(i + " = " + evtObj[i]);
		}
		
		loadAlbumsImage(evtObj.feed);
	}
	
	private function handlerFeedOnError(evtObj:Object):Void{
		for(var i:String in evtObj){
			trace(i + " = " + evtObj[i]);
		}
	}
	
	private function handlerEntryOnLoad(evtObj:Object):Void{
		for(var i:String in evtObj){
			trace(i + " = " + evtObj[i]);
		}
		
		loadPhotosImage(evtObj.feed);
	}
	
	private function handlerEntryOnError(evtObj:Object):Void{
		for(var i:String in evtObj){
			trace(i + " = " + evtObj[i]);
		}
	}
	
	private function loadAlbumsImage(feed:UserFeed):Void{
		
		albums_sp.contentPath = "albums_mc";
		
		for(var i:Number = 0, j:Number = 0; i<feed.entrys.length; i++){
			var entry:AlbumEntry = feed.entrys[i];
			if(entry.rights.toString() == "public"){
				albums_sp.content.createClassObject(mx.controls.Loader, "album" + j + "_ldr", 100+j);
				var my_ldr:Loader = albums_sp.content["album" + j + "_ldr"];
				
				my_ldr.move((100+10)*j, 0);
				my_ldr.autoLoad = false;
				my_ldr.contentPath = entry.media.thumbnail;
				my_ldr.addEventListener("complete", {complete:Delegate.create(this, function(eventObj:Object){
																	   albums_sp.redraw(true);
																	   })});
				my_ldr.load();
				
				my_ldr["service"] = __service;
				my_ldr["entry"] = entry;
				my_ldr.onRelease = selectAlbum;
				
				j++;
			}
		}
	}
	
	private function selectAlbum():Void{
		var service:PicasawebService = this["service"];
		var entry:AlbumEntry = this["entry"];
		
		service.loadEntry(entry);
	}
	
	private function loadPhotosImage(feed:AlbumFeed):Void{
		
		photos_sp.contentPath = "photos_mc";
		
		for(var i:Number = 0, x:Number = 0, y:Number = 0; i<feed.entrys.length; i++){
			var entry:PhotoEntry = feed.entrys[i];
			photos_sp.content.createClassObject(mx.controls.Loader, "photo" + i + "_ldr", 100+i);
			var my_ldr:Loader = photos_sp.content["photo" + i + "_ldr"];
				
			my_ldr.move((100+10)*x, (100+10)*y);
			my_ldr.autoLoad = false;
			my_ldr.contentPath = entry.content;
			my_ldr.addEventListener("complete", {complete:Delegate.create(this, function(eventObj:Object){
																	   photos_sp.redraw(true);
																	   })});
			my_ldr.load();
			
			x++;
			if(x==5){
				x = 0;
				y++;
			}
		}
	}
}